package com.aoasaa;


import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class VoiceRecorderActivity extends Activity {

	MediaRecorder recorder;
	File audiofile = null;
	private static final String TAG = "SoundRecordingActivity";
	private View startButton;
	private View stopButton; 
                  
	public SharedPreferences sp_obj;
	public Editor editor_obj;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recordingscreen);
		startButton = findViewById(R.id.start);
		stopButton = findViewById(R.id.stop);


		// accessing shared preference       
		sp_obj=getSharedPreferences("ArchiveApp",MODE_WORLD_WRITEABLE);
		editor_obj=sp_obj.edit();

		/*when clicked on next button will navigate to 
    uploading voice notes screen
		 */
		Button nextbtn=(Button)findViewById(R.id.btn_recordnext);
		nextbtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v)  
			{
				Intent i=new Intent(VoiceRecorderActivity.this,UploadingVoiceNotes.class);
				startActivity(i);
			}
		});
	}

	public void startRecording(View view) throws IOException {

		startButton.setEnabled(false);
		stopButton.setEnabled(true);

		File sampleDir = Environment.getExternalStorageDirectory();
		try {
			audiofile = File.createTempFile("sound", ".3gp", sampleDir);
		} catch (IOException e) {
			Log.e(TAG, "sdcard access error");
			return; 
		}
		recorder = new MediaRecorder();
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setOutputFile(audiofile.getAbsolutePath());
		recorder.prepare();
		recorder.start();
	}          
   
	public void stopRecording(View view) {
		startButton.setEnabled(true);
		stopButton.setEnabled(false);
		recorder.stop();
		recorder.release();    
		addRecordingToMediaLibrary();
	}
 
	protected void addRecordingToMediaLibrary() {
		ContentValues values = new ContentValues(4); 
		long current = System.currentTimeMillis();
		values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
		values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
		values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
		values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
		ContentResolver contentResolver = getContentResolver();

		Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		Uri newUri = contentResolver.insert(base, values);
                
		sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
		//Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
		Toast.makeText(this, "Audio file name " + audiofile.getName()+" path "+audiofile.getAbsolutePath(), Toast.LENGTH_LONG).show();

		editor_obj.putString("SelectedFile",audiofile.getName().toString());
		editor_obj.putString("uploadsessionfileurl", audiofile.getAbsolutePath().toString());
		editor_obj.commit();
		 
		 
		 
	}   
} 